// pages/movie/movie.js
const app=getApp();
const request=require("../../utils/request")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    playData:null,
    headVideo:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(!options.mid) wx.navigateTo({
      url: '/pages/recommend/recommend',
    });
    wx.showLoading({
      title: '正在加载...',
    })
    let mid=options.mid;//获取传递的id.
    //获取小视频详情
    wx.request({
      url: 'https://api.kele8.cn/agent?url=https://app.vmovier.com/apiv3/post/view?postid='+mid,
      method:"GET",
      data:{},
      success:(re)=>{
        wx.hideLoading();
        if(!re||!re.data||!re.data.data){
          wx.showToast({
            title: '加载失败...',
          });
        }  
        let duration=re.data.data.duration;
        let second=(duration);
        let minute=parseInt(second/60);
        minute=minute<10?"0"+minute:minute;
        let seconds=parseInt(second%60);
        seconds=seconds<10?"0"+seconds:seconds;
        re.data.data.strduration=`${minute}'${seconds}'`;
        this.setData({
          playData:re.data.data
        })
      }
    });
    request.get("/post/getPostInCate?p=1&size=10&cateid=6").then(re=>{
      this.setData({
        headVideo:re
      })
    })
  },
  addCollect(){
    if(app.globalData.userInfo){
      const database=wx.cloud.database();//获取当前云环境的数据库对象
      const movieCollection=database.collection("movie_store");//获取实例集合
      movieCollection.where({
        postid:this.data.playData.postid
      }).get().then(result=>{
        if(result.data.length>0){
          wx.showToast({
            title: '不可重复收藏',
          });
        }else{
          this.data.playData.collection=new Date().getTime();
          movieCollection.add({data:this.data.playData}).then(result=>{
            wx.showToast({
              title: '收藏成功',
            })
          }).catch(er=>{
            wx.showToast({
              title: '收藏失败',
            })
          })
        }
      })
    }else{
      wx.showToast({
        icon:"error",
        title: '请先登录',
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})